package cn.bdqn.dao;

import cn.bdqn.entity.Student;

import java.sql.*;

//数据层的父类
public abstract class BaseDao {
    //查询方法
    public String executeQuery(String sql){
        Student student = null;
        try {
            //注册
            Class.forName("com.mysql.jdbc.Driver");
            String connster = ("jdbc:mysql:///myschool?characterEncoding=utf8");
            //创建连接对象
            Connection conn = DriverManager.getConnection(connster, "root", "root");
            //创建表达式对象
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            student = null;
            if (rs.next()) {
                student = new Student();
                student.setStudentNo(rs.getString("StudentNo"));
                student.setLoginPwd(rs.getString("LoginPwd"));
                student.setStudentName(rs.getString("StudentName"));
                student.setSex(rs.getString("Sex"));
                student.setGradeId(rs.getInt("GradeId"));
                student.setPhone(rs.getString("Phone"));
                student.setAddress(rs.getString("Address"));
                student.setBornDate(rs.getDate("BornDate"));
                student.setEmail(rs.getString("Email"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //非查询方法
    public int executeNunQuery(String sql){
        int rs=0;
        try {
            //注册
            Class.forName("com.mysql.jdbc.Driver");
            String connster = ("jdbc:mysql:///myschool?characterEncoding=utf8");
            //创建连接对象
            Connection conn = DriverManager.getConnection(connster, "root", "root");
            //创建表达式对象
            Statement stmt = conn.createStatement();
            //执行SQL语句，返回一个结果集对象
            rs = stmt.executeUpdate(sql);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
}
